<template>
  <el-dialog
    v-model="visitEditDialogVisible"
    title="修改抵押信息"
    width="40%"
    center
    @close="closeDialog(false)"
  >
    <el-form
      ref="validateForm"
      :model="customerForm"
      :rules="rules"
      label-width="120px"
      :status-icon="true"
    >

        <el-form-item label="客户" prop="visit">
        {{customerName}}
      </el-form-item>
      <el-form-item label="联系状态" prop="currentState">
        <el-select v-model="customerForm.currentState" placeholder="请选择">
          <el-option
            v-for="item in connetStates"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>  
       <el-form-item label="回访信息" prop="result">
        <el-input v-model="customerForm.result"  ></el-input>
      </el-form-item>

   <!-- <el-form-item label="回访时间" prop="visitDate">
        <el-date-picker v-model="customerForm.visitDate" value-format="YYYY-MM-DD" format="YYYY-MM-DD" type="date" placeholder="选择时间">
      </el-date-picker>
      </el-form-item> -->
    
      <el-form-item>
        <el-button type="primary" @click="submitForm">修改</el-button>
        <el-button
          @click="
            () => {
              closeDialog(false);
            }
          "
          >取消</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from "vue";
import { ElMessage } from "element-plus";
import { putRequest,getRequest } from "../../utils/api";

function customerAdd(state, validateForm, emit) {
  const submitForm = () => {
    validateForm.value.validate((valid) => {
      if (valid) {
        if(state.customerForm.customerId){
          putRequest("/sell/customer/rvisit/", state.customerForm).then((res) => {
          if (res && res.code == 200) {
            validateForm.value.resetFields();
            emit("onCloseEditVisitVisible", false, res.data);
          } else {
            ElMessage({
              showClose: true,
              message: res.message,
              type: "error",
            });
          }
        });
        }else{
           ElMessage({
              showClose: true,
              message: '未选中经销商',
              type: "error",
            });
        }
        
      }
    });
  };
  return { submitForm };
}

 

export default {
  components: {},
  props: {
    visitId:String,
    visitEditDialogVisible: Boolean,
     customerId:String,
    currentState:String,
    customerName:String
  },

  setup(props, { emit }) {
    const validateForm = ref();
    const state = reactive({
      visitEditDialogVisible: props.visitEditDialogVisible,
       connetStates: [
        {
          value: 0,
          label: "待沟通",
        },
        {
          value: 1,
          label: "沟通中",
        },
        {
          value: 2,
          label: "已下单",
        },
        {
          value: 3,
          label: "战败",
        },{
          value: 4,
          label: "停机",
        },{
          value: 5,
          label: "无效信息",
        },
      ],
      customerForm: {
        currentState:props.currentState,
        customerId:props.customerId,
        // visitDate:formatNowDate(),
        id:props.visitId
      },
     customerName:props.customerName,
    });

    const rules = {
      currentState: [{ required: true, message: "请选择客户状态", trigger: "blur" }]
     
    };

    const closeDialog = (visible, data) => {
      validateForm.value.resetFields();
      emit("onCloseEditVisitVisible", visible, data);
    };
   
    const loadVisitData = ()=>{
         getRequest("/sell/customer/rvisit/one", { id: state.customerForm.id }).then((res) => {
            if (res && res.code == 200) {
            state.customerForm = res.data;
            } else {
            ElNotification({
                type: "error",
                title: "失败",
                message: "查询失败",
                duration: 2000,
            });
            }

     
        });
    }
    onMounted(()=>{
      loadVisitData()
    })
    return {
      ...toRefs(state),
      closeDialog,
      rules,
      ...customerAdd(state, validateForm, emit), 
      validateForm
   
    };
  },
};
</script>

<style>

.el-input-number__increase{
  margin-top:1px;
}

.el-input-number__decrease{
  margin-top:1px;
}


.el-form-item__error {
  padding-top: 0px;
}
.el-input__inner {
  height: 32px;
  line-height: 32px;
}
.el-form-item {
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 18px;
  color: #8c939d;
  width: 88px;
  height: 88px;
  line-height: 88px;
  text-align: center;
}
.avatar {
  width: 88px;
  height: 88px;
  display: block;
}
</style>